package com.gorkr.test.service;

import com.gorkr.test.pojo.Hotel;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author gorkr
 * @date 2022/04/07 22:30
 **/
@Service
public class EsService {
//
//    @Autowired
//    RestHighLevelClient client;
//
//    public List<Hotel> getHotelFromTitle(String keyword){
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        // 构建query
//        searchSourceBuilder.query(QueryBuilders.matchQuery("title",keyword));
//        searchRequest.source(searchSourceBuilder);
//        try {
//            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//            RestStatus status = searchResponse.status();
//            if(status!=RestStatus.OK){
//                return null;
//            }
//            SearchHits hits = searchResponse.getHits();
//            for (SearchHit hit: hits
//                 ) {
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//            }
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Autowired
    EsRepository esRepository;
    public List<Hotel> getHotelFromTitle(String keyword){
        return esRepository.findByTitleLike(keyword);
    }
}
    